package com.example.blog.controller;

import com.example.blog.dto.SystemLogDTO;
import com.example.blog.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/api/logs")
@CrossOrigin(origins = "*", maxAge = 3600)
@PreAuthorize("hasRole('ADMIN')")
public class SystemLogController {
    @Autowired
    private SystemLogService systemLogService;

    @PostMapping
    public ResponseEntity<SystemLogDTO> createLog(@RequestBody SystemLogDTO logDTO) {
        return ResponseEntity.ok(systemLogService.createLog(logDTO));
    }

    @GetMapping("/{id}")
    public ResponseEntity<SystemLogDTO> getLogById(@PathVariable Long id) {
        return ResponseEntity.ok(systemLogService.getLogById(id));
    }

    @GetMapping
    public ResponseEntity<Page<SystemLogDTO>> getAllLogs(
            @RequestParam(required = false) Map<String, String> filters,
            Pageable pageable) {
        return ResponseEntity.ok(systemLogService.getAllLogs(filters, pageable));
    }

    @GetMapping("/type/{type}")
    public ResponseEntity<Page<SystemLogDTO>> getLogsByType(
            @PathVariable String type,
            Pageable pageable) {
        return ResponseEntity.ok(systemLogService.getLogsByType(type, pageable));
    }

    @GetMapping("/level/{level}")
    public ResponseEntity<Page<SystemLogDTO>> getLogsByLevel(
            @PathVariable String level,
            Pageable pageable) {
        return ResponseEntity.ok(systemLogService.getLogsByLevel(level, pageable));
    }

    @GetMapping("/module/{module}")
    public ResponseEntity<Page<SystemLogDTO>> getLogsByModule(
            @PathVariable String module,
            Pageable pageable) {
        return ResponseEntity.ok(systemLogService.getLogsByModule(module, pageable));
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<Page<SystemLogDTO>> getLogsByUser(
            @PathVariable Long userId,
            Pageable pageable) {
        return ResponseEntity.ok(systemLogService.getLogsByUser(userId, pageable));
    }

    @GetMapping("/date-range")
    public ResponseEntity<Page<SystemLogDTO>> getLogsByDateRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end,
            Pageable pageable) {
        return ResponseEntity.ok(systemLogService.getLogsByDateRange(start, end, pageable));
    }

    @DeleteMapping("/cleanup")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    public ResponseEntity<?> cleanupOldLogs(@RequestParam(defaultValue = "30") int days) {
        systemLogService.cleanupOldLogs(days);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/stats")
    public ResponseEntity<Map<String, Long>> getLogStats() {
        return ResponseEntity.ok(systemLogService.getLogStats());
    }
} 